package org.edu.modules.logistics.bx.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.dto.ExcelPo;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.logistics.bx.dto.VeHqBaoxiuDto;
import org.edu.modules.logistics.bx.entity.VeHqBaoxiu;
import org.edu.modules.logistics.bx.service.VeHqBaoxiuService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = {"a-报修记录表"})
@RestController
@RequestMapping("/veHqBaoxiu")
@Slf4j
public class VeHqBaoxiuController{

    @Autowired
    private VeHqBaoxiuService veHouqinBaoxiuService;

    @ApiOperation("分页查询a-报修记录表")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeHqBaoxiu> page) {
        QueryWrapper<VeHqBaoxiu> queryWrapper =  page.buildQueryWrapper(VeHqBaoxiu.class);
        return Result.OK(veHouqinBaoxiuService.page(page, queryWrapper));
    }



    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeHqBaoxiu> page, HttpServletResponse resp) {
        QueryWrapper<VeHqBaoxiu> queryWrapper =  page.buildQueryWrapper(VeHqBaoxiu.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("物品名称","name"));
        list.add(new ExcelPo("维修结果：1=完成、2=在修，0=未修","status"));
        list.add(new ExcelPo("维修方式","type"));
        list.add(new ExcelPo("备注","remark"));
        list.add(new ExcelPo("微信地点","place"));
        list.add(new ExcelPo("申请人","applyUser"));
        list.add(new ExcelPo("申请人","applyUserName"));
        list.add(new ExcelPo("经手人","jsUser"));
        list.add(new ExcelPo("经手人","jbUserName"));
        list.add(new ExcelPo("批准人","pjUser"));
        list.add(new ExcelPo("批准人","pjUserName"));
                PoiUtils.downloadFile(resp,veHouqinBaoxiuService.page(page, queryWrapper).getRecords(),list);
        return Result.OK();
    }

    @AutoLog(value = "a-报修记录表-分页列表查询")
    @ApiOperation(value="a-报修记录表-分页列表查询", notes="a-报修记录表-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeHqBaoxiu entity,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeHqBaoxiu> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeHqBaoxiu> page = new PageDto<VeHqBaoxiu>(pageNo, pageSize);
        IPage<VeHqBaoxiu> pageList = veHouqinBaoxiuService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        VeHqBaoxiu dto = veHouqinBaoxiuService.findDtoById(id);
        if(dto==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeHqBaoxiuDto dto) {
        veHouqinBaoxiuService.insertOrUpdate(dto , true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value="编辑", notes="编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeHqBaoxiuDto dto) {
        veHouqinBaoxiuService.insertOrUpdate(dto , false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        veHouqinBaoxiuService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value="更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        LambdaUpdateWrapper<VeHqBaoxiu> updateWrapper = new LambdaUpdateWrapper<>();
        if(statusDto.getIds()!=null && statusDto.getIds().size()>0){

            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();


            updateWrapper.set(VeHqBaoxiu::getStatus,statusDto.getStatus());
            updateWrapper.in(VeHqBaoxiu::getId,statusDto.getIds());

            if(statusDto.getStatus()==0){
                updateWrapper.set(VeHqBaoxiu::getPjUserName,sysUser.getRealname());
                updateWrapper.set(VeHqBaoxiu::getPjUser,sysUser.getId());
                updateWrapper.set(VeHqBaoxiu::getAuditRemark,statusDto.getMsg());
            }else{
                updateWrapper.set(VeHqBaoxiu::getWxRemark,statusDto.getMsg());
            }

            veHouqinBaoxiuService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        }else{
            return Result.error("未选择!");
        }
    }



    @AutoLog(value = "更新状态")
    @ApiOperation(value="更新状态")
    @PostMapping(value = "/updateBaoxiu")
    public Result<?> updateBaoxiu(@RequestBody VeHqBaoxiuDto baoxiuDto) {
        LambdaUpdateWrapper<VeHqBaoxiu> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(VeHqBaoxiu::getStatus,baoxiuDto.getStatus());
            updateWrapper.set(VeHqBaoxiu::getJbUserName,baoxiuDto.getJbUserName());
            updateWrapper.set(VeHqBaoxiu::getJsUser,baoxiuDto.getJsUser());
            updateWrapper.set(VeHqBaoxiu::getAuditRemark,baoxiuDto.getAuditRemark());
            updateWrapper.eq(VeHqBaoxiu::getId,baoxiuDto.getId());

            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            updateWrapper.set(VeHqBaoxiu::getPjUser,sysUser.getId());
            updateWrapper.set(VeHqBaoxiu::getPjUserName,sysUser.getRealname());
            veHouqinBaoxiuService.update(updateWrapper);
            return Result.OK("批量更新成功!");
    }




}
